<template>
  <div>
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>修改密码</span>
        </div>
      </template>
      <el-form :model="data.form" label-width="100px" style="max-width: 500px; margin: 0 auto;">
        <el-form-item label="原密码">
          <el-input v-model="data.form.oldPassword" type="password" show-password />
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="data.form.newPassword" type="password" show-password />
        </el-form-item>
        <el-form-item label="确认新密码">
          <el-input v-model="data.form.confirmPassword" type="password" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确认修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { ElMessage } from "element-plus";
import { updateMerchantPassword } from "@/api/index.js";

const data = reactive({
  form: {
    oldPassword: '',
    newPassword: '',
    confirmPassword: ''
  }
})

const submitForm = async () => {
  if (!data.form.oldPassword) {
    ElMessage.error('请输入原密码')
    return
  }
  if (!data.form.newPassword) {
    ElMessage.error('请输入新密码')
    return
  }
  if (data.form.newPassword !== data.form.confirmPassword) {
    ElMessage.error('两次输入的新密码不一致')
    return
  }
  
  try {
    // 调用修改商家密码接口
    const res = await updateMerchantPassword({
      oldPassword: data.form.oldPassword,
      newPassword: data.form.newPassword
    });
    
    if (res.code === 200 && res.data) {
      ElMessage.success('密码修改成功');
      // 清空表单
      data.form = {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      };
    } else {
      ElMessage.error(res.message || '密码修改失败');
    }
  } catch (error) {
    ElMessage.error('密码修改失败: ' + error.message);
  }
}
</script>

<style scoped>
.card-header {
  font-size: 18px;
  font-weight: bold;
}
</style>